package demo1;

import java.util.Arrays;
import java.util.Comparator;
class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        if(this.age - o.age > 0) {
            return 1;
        }else if(this.age - o.age < 0) {
            return -1;
        }else {
            return 0;
        }
        //return 0;
    }
}
public class Test1 {
    public static void main(String[] args) {
        Student student1 = new Student("bit",10);
        Student student2 = new Student("hello",40);

        if(student1.compareTo(student2) > 0) {
            System.out.println("student1 > student2");
        }else {
            System.out.println("student1 < student2");
        }
    }
    public static void main2(String[] args) {
        String[] strings = {"abc","hello","bcd"};
        Arrays.sort(strings);
        System.out.println(Arrays.toString(strings));
    }
    //这个main6方法会报类型转换异常
    public static void main6(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("bit",10);
        students[1] = new Student("hello",40);
        students[2] = new Student("gbc",5);

        Arrays.sort(students);

        System.out.println(Arrays.toString(students));
    }
    public static void main1(String[] args) {
        int[] array = {1,4,2,7,3,8,5};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
}
